import 'package:flame/game.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import 'mygame.dart';

class EndGame extends StatelessWidget {
  EndGame({Key? key, required this.game})
      : super(key: key);
  static const String menuId = 'EndGame';
  final MyGame game;
  final double score = 0;
  final TextStyle shadowStyle = const TextStyle(
      fontSize: 24, shadows: [Shadow(color: Colors.white, blurRadius: 10)]);
  final GameController gameController =
      Get.put<GameController>(GameController());

  @override
  Widget build(BuildContext context) {
    return Material(
      color: Colors.transparent,
      child: Center(
        child: Container(
          color: Colors.black26,
          alignment: Alignment.center,
          padding: const EdgeInsets.symmetric(vertical: 20),
          child: Wrap(
            spacing: 30,
            direction: Axis.vertical,
            crossAxisAlignment: WrapCrossAlignment.center,
            children: [
              Text(
                '游戏结束',
                style: shadowStyle,
              ),
              Obx(() => Text('时间: ${gameController.score}秒',
                  style: TextStyle(
                      fontSize: 20,
                      shadows: [Shadow(color: Colors.white, blurRadius: 10)]))),
              ElevatedButton(onPressed: _restart, child: const Text('重新开始')),
              ElevatedButton(
                  onPressed: () {
                    Get.back();
                    gameController.gameEnd();
                  },
                  child: const Text('退出游戏'))
            ],
          ),
        ),
      ),
    );
  }

  void _restart() {
    gameController.gameEnd();
    game.overlays.remove(menuId);
    game.resumeEngine();
    game.mainCircle.position.setValues(game.canvasSize.x / 2, game.canvasSize.y / 2);
    game.mainCircle.onPanUpdate(Vector2(game.canvasSize.x / 2, game.canvasSize.y / 2));
    game.bullets = [];
    game.isRunning = true;
    game.counts = 0;
  }
}
